/**********************************************************************
 * Project : WoWGuildRoster (http://www.codeplex.com/WowGuildRoster/)
 * Module  : DotNetWarcraft.Common.SqlDataUtils
 **********************************************************************
 * 
 * 
 * 
 **********************************************************************
 * License : All code is subject to licensing terms per the Microsoft
 *   Permissive License (Ms-PL) v1.1. For full license information,
 *   refer to the packaged license.txt file.
 **********************************************************************
 * Revision History:
 * -----------------
 * 2007-08-26 -- Cliff Simpkins: Initial Version
 **********************************************************************
 */
using System;
using System.Collections.Generic;
using System.Text;

namespace DotNetWarcraft.Common {
  public class SqlDataUtils {

    #region "Public Static Methods"
    //public static object GetNull(object Field) {
    //  return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value);
    //}

    public static object ValidateDbValue(object Field) {
      if (Field is DateTime) {
        if ((DateTime)Field == DateTime.MinValue ||
            (DateTime)Field == DateTime.MaxValue ||
            (DateTime)Field == DateTime.Parse(System.Data.SqlTypes.SqlDateTime.MinValue.ToString())) {
          return DBNull.Value;
        }
      } else if (Field is int) {
        if ((int)Field == -1) {
          return DBNull.Value;
        }
      } else if (Field is double) {
        if ((double)Field == -1.0) {
          return DBNull.Value;
        }
      } else if (Field is string) {
        if (((string)Field).Trim() == "") {
          return DBNull.Value;
        } else {
          return Field.ToString().Trim();
        }
      } else if (Field is String) {
        if (((String)Field).Trim() == "") {
          return DBNull.Value;
        } else {
          return Field.ToString().Trim();
        }
      }
      return Field;
      //        case (new DateTime()).GetType(): return Field;
      //        case Int32: return Field;
      //        case String: return Field;
      //        default: return Field;
      //      }
    }

    public static object RetrieveDbValue(object Field, object defaultValue) {
      if (Field == DBNull.Value) {
        return defaultValue;
      } else {
        return Field;
      }
    }

    public static String RetrieveDbString(object Field) {
      if (Field == DBNull.Value) {
        return "";
      } else {
        return Field.ToString();
      }
    }

    public static DateTime RetrieveDbDate(object Field) {
      if (Field == DBNull.Value) {
        return DateTime.MinValue;
      } else if (Field is String) {
        return DateTime.Parse(Field.ToString());
      } else {
        return (DateTime)Field;
      }
    }

    public static int RetrieveDbInt(object Field) {
      if (Field == DBNull.Value) {
        return -1;
      } else if (Field is String) {
        return int.Parse(Field.ToString());
      } else {
        return (int)Field;
      }
    }

    public static double RetrieveDbDec(object Field) {
      if (Field == DBNull.Value) {
        return -1.0;
      } else if (Field is String) {
        double _outVal;
        if (double.TryParse(Field.ToString(), out _outVal)) {
          return _outVal;
        } else return -1.0;
      } else {
        return (double)Field;
      }
    }

    #endregion

  }
}
